home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
JFC.bin
/
SwingUtilities.java
< prev
next >
Wrap
Text File
|
1998-06-30
|
51KB
|
1,376 lines
/*
* @(#)SwingUtilities.java 1.54 98/06/23
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Hashtable;
import java.lang.reflect.*;
import com.sun.java.accessibility.*;
/**
* A collection of utility methods for Swing.
*
* @version 1.54 06/23/98
*/
public class SwingUtilities implements SwingConstants {
// These states are system-wide, rather than AppContext wide.
private static boolean canAccessEventQueue = false;
private static boolean eventQueueTested = false;
/** Return true if <code>a</code> contains <code>b</code> **/
public static final boolean isRectangleContainingRectangle(Rectangle a,Rectangle b) {
if (b.x >= a.x && (b.x + b.width) <= (a.x + a.width) &&
b.y >= a.y && (b.y + b.height) <= (a.y + a.height)) {
return true;
}
return false;
}
/** Return the rectangle (0,0,bounds.width,bounds.height) for the component <code>aComponent</code>
*/
public static Rectangle getLocalBounds(Component aComponent) {
Rectangle b = new Rectangle(aComponent.getBounds());
b.x = b.y = 0;
return b;
}
/**
* @return the first Window ancestor of c
*/
private static Window getWindowAncestor(Component c) {
for(Container p = c.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
return (Window)p;
}
}
return null;
}
/** Convert a <code>aPoint</code> in <code>source</code> coordinate system to
* <code>destination</code> coordinate system.
* If <code>source></code>is null,<code>aPoint</code> is assumed to be in <code>destination</code>'s
* root component coordinate system.
* If <code>destination</code>is null, <code>aPoint</code> will be converted to <code>source</code>'s
* root component coordinate system.
* If both <code>source</code> and <code>destination</code> are null, return <code>aPoint</code>
* without any conversion.
*/
public static Point convertPoint(Component source,Point aPoint,Component destination) {
Point p;
if(source == null && destination == null)
return aPoint;
if(source == null) {
source = getWindowAncestor(destination);
if(source == null)
throw new Error("Source component not connected to component tree hierarchy");
}
p = new Point(aPoint);
convertPointToScreen(p,source);
if(destination == null) {
destination = getWindowAncestor(source);
if(destination == null)
throw new Error("Destination component not connected to component tree hierarchy");
}
convertPointFromScreen(p,destination);
return p;
}
/** Convert the point <code>(x,y)</code> in <code>source</code> coordinate system to
* <code>destination</code> coordinate system.
* If <code>source></code>is null,<code>(x,y)</code> is assumed to be in <code>destination</code>'s
* root component coordinate system.
* If <code>destination</code>is null, <code>(x,y)</code> will be converted to <code>source</code>'s
* root component coordinate system.
* If both <code>source</code> and <code>destination</code> are null, return <code>(x,y)</code>
* without any conversion.
*/
public static Point convertPoint(Component source,int x, int y,Component destination) {
Point point = new Point(x,y);
return convertPoint(source,point,destination);
}
/** Convert the rectangle <code>aRectangle</code> in <code>source</code> coordinate system to
* <code>destination</code> coordinate system.
* If <code>source></code>is null,<code>aRectangle</code> is assumed to be in <code>destination</code>'s
* root component coordinate system.
* If <code>destination</code>is null, <code>aRectangle</code> will be converted to <code>source</code>'s
* root component coordinate system.
* If both <code>source</code> and <code>destination</code> are null, return <code>aRectangle</code>
* without any conversion.
*/
public static Rectangle convertRectangle(Component source,Rectangle aRectangle,Component destination) {
Point point = new Point(aRectangle.x,aRectangle.y);
point = convertPoint(source,point,destination);
return new Rectangle(point.x,point.y,aRectangle.width,aRectangle.height);
}
/** Convience method for searching above <code>comp</code> in the
* component hierarchy and returns the first object of class <code>c</code> it
* finds. Can return null, if a class <code>c</code> cannot be found.
*/
public static Container getAncestorOfClass(Class c, Component comp) {
if(comp == null || c == null)
return null;
Container parent = comp.getParent();
while(parent != null && !(c.isInstance(parent)))
parent = parent.getParent();
return parent;
}
/** Convience method for searching above <code>comp</code> in the
* component hierarchy and returns the first object of <code>name</code> it
* finds. Can return null, if <code>name</code> cannot be found.
*/
public static Container getAncestorNamed(String name, Component comp) {
if(comp == null || name == null)
return null;
Container parent = comp.getParent();
while(parent != null && !(name.equals(parent.getName())))
parent = parent.getParent();
return parent;
}
/**
* Returns the deepest child Component of parent that is at the location
* <code>x</code>, <code>y</code>. If <code>parent</code> is not a Container, it is
* returned, otherwise this method is messaged again with the child
* component at <code>x</code>, <code>y</code>.
*/
public static Component getDeepestComponentAt(Component parent, int x,
int y) {
if(parent != null && parent instanceof Container) {
Component child = ((Container)parent).getComponentAt(x, y);
if(child != null && child != parent && child.isVisible()) {
Rectangle b = child.getBounds();
child = getDeepestComponentAt(child, x - b.x,y-b.y);
if(child != null)
return child;
}
}
return parent;
}
/** Returns a MouseEvent similar to <code>sourceEvent</code> except that its x
* and y members have been converted to <code>destination</code>'s coordinate
* system. If <code>source</code> is null, <code>sourceEvent</code> x and y members
* are assumed to be into <code>destination<code>'s root component coordinate system.
* If <code>destination</code> is <code>null</code>, the
* returned MouseEvent will be in <code>source</code>'s coordinate system.
* <code>sourceEvent</code> will not be changed. A new event is returned.
* the <code>source</code> field of the returned event will be set
* to <code>destination</code> if destination is non null
* use the translateMouseEvent() method to translate a mouse event from
* one component to another without changing the source.
*/
public static MouseEvent convertMouseEvent(Component source,
MouseEvent sourceEvent,
Component destination) {
Point p = convertPoint(source,new Point(sourceEvent.getX(),
sourceEvent.getY()),
destination);
Component newSource;
if(destination != null)
newSource = destination;
else
newSource = source;
return new MouseEvent(newSource,
sourceEvent.getID(),
sourceEvent.getWhen(),
sourceEvent.getModifiers(),
p.x,p.y,
sourceEvent.getClickCount(),
sourceEvent.isPopupTrigger());
}
public static void convertPointToScreen(Point p,Component c) {
Rectangle b;
int x,y;
do {
if(c instanceof JComponent) {
x = ((JComponent)c).getX();
y = ((JComponent)c).getY();
} else if(c instanceof java.applet.Applet) {
Point pp = c.getLocationOnScreen();
x = pp.x;
y = pp.y;
} else {
b = c.getBounds();
x = b.x;
y = b.y;
}
p.x += x;
p.y += y;
if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
break;
c = c.getParent();
} while(c != null);
}
public static void convertPointFromScreen(Point p,Component c) {
Rectangle b;
int x,y;
do {
if(c instanceof JComponent) {
x = ((JComponent)c).getX();
y = ((JComponent)c).getY();
} else if(c instanceof java.applet.Applet) {
Point pp = c.getLocationOnScreen();
x = pp.x;
y = pp.y;
} else {
b = c.getBounds();
x = b.x;
y = b.y;
}
p.x -= x;
p.y -= y;
if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
break;
c = c.getParent();
} while(c != null);
}
/** Return <code>aComponent</code>'s window **/
public static Window windowForComponent(Component aComponent) {
for (Container p = aComponent.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
return (Window)p;
}
}
return null;
}
/** Return <code>true</code> if a component <code>a</code> descends from a component <code>b</code>
*/
public static boolean isDescendingFrom(Component a,Component b) {
if(a == b)
return true;
for(Container p = a.getParent();p!=null;p=p.getParent())
if(p == b)
return true;
return false;
}
/**
* Convenience to calculate an intersection of two rectangles without allocating a new rectangle
* Return dest.
*/
public static Rectangle computeIntersection(int x,int y,int width,int height,Rectangle dest) {
int x1 = (x > dest.x) ? x : dest.x;
int x2 = ((x+width) < (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
int y1 = (y > dest.y) ? y : dest.y;
int y2 = ((y + height) < (dest.y + dest.height) ? (y+height) : (dest.y + dest.height));
dest.x = x1;
dest.y = y1;
dest.width = x2 - x1;
dest.height = y2 - y1;
return dest;
}
/**
* Convenience to calculate the union of two rectangles without allocating a new rectangle
* Return dest
*/
public static Rectangle computeUnion(int x,int y,int width,int height,Rectangle dest) {
int x1 = (x < dest.x) ? x : dest.x;
int x2 = ((x+width) > (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
int y1 = (y < dest.y) ? y : dest.y;
int y2 = ((y+height) > (dest.y + dest.height)) ? (y+height) : (dest.y + dest.height);
dest.x = x1;
dest.y = y1;
dest.width = (x2 - x1);
dest.height= (y2 - y1);
return dest;
}
/**
* Convenience returning an array of rect representing the regions within
* <code>rectA</code> that do not overlap with <code>rectB</code>. If the
* two Rects do not overlap, returns an empty array
*/
public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) {
if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) {
return new Rectangle[0];
}
Rectangle t = new Rectangle();
Rectangle a=null,b=null,c=null,d=null;
Rectangle result[];
int rectCount = 0;
/* rectA contains rectB */
if (isRectangleContainingRectangle(rectA,rectB)) {
t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height;
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y;
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width;
t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
t.x = rectB.x + rectB.width; t.y = rectA.y; t.width = rectA.x + rectA.width - (rectB.x + rectB.width);
t.height = rectA.height;
if(t.width > 0 && t.height > 0) {
d = new Rectangle(t);
rectCount++;
}
} else {
/* 1 */
if (rectB.x <= rectA.x && rectB.y <= rectA.y) {
if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
t.x = rectA.x; t.y = rectB.y + rectB.height;
t.width = rectA.width; t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else {
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width),
(rectB.y + rectB.height) - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x <= rectA.x && (rectB.y + rectB.height) >= (rectA.y + rectA.height)) {
if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectB.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width),
(rectA.y + rectA.height) - rectB.y);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x <= rectA.x) {
if ((rectB.x + rectB.width) >= (rectA.x + rectA.width)) {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width>0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectB.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width),
rectB.height);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x <= (rectA.x + rectA.width) && (rectB.x + rectB.width) > (rectA.x + rectA.width)) {
if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = t;
rectCount++;
}
} else if (rectB.y <= rectA.y) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x,
(rectB.y + rectB.height) - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
(rectA.y + rectA.height) - rectB.y);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
rectB.height);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
}
} else if (rectB.x >= rectA.x && (rectB.x + rectB.width) <= (rectA.x + rectA.width)) {
if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
} else if (rectB.y <= rectA.y) {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectB.x, (rectB.y + rectB.height),
rectB.width,
(rectA.y + rectA.height) - (rectB.y + rectB.height));
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
} else {
t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.setBounds(rectB.x, rectA.y, rectB.width,
rectB.y - rectA.y);
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.setBounds((rectB.x + rectB.width), rectA.y,
(rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
}
}
}
result = new Rectangle[rectCount];
rectCount = 0;
if(a != null)
result[rectCount++] = a;
if(b != null)
result[rectCount++] = b;
if(c != null)
result[rectCount++] = c;
if(d != null)
result[rectCount++] = d;
return result;
}
public static boolean isLeftMouseButton(MouseEvent anEvent) {
return ((anEvent.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK ||
(anEvent.getModifiers() == 0));/*PENDING(ARNAUD) this is to workaround a bug on Solaris */
}
public static boolean isMiddleMouseButton(MouseEvent anEvent) {
return ((anEvent.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK);
}
public static boolean isRightMouseButton(MouseEvent anEvent) {
return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
}
/*
* Returns whether this is being run on a JDK 1.2 or later VM.
* This is a system-wide, rather than AppContext-wide, state.
*/
/*package-private*/ static boolean is1dot2 = true;
static {
try {
// Test if method introduced in 1.2 is available.
Method m = Toolkit.class.getMethod("getMaximumCursorColors", null);
is1dot2 = (m != null);
} catch (NoSuchMethodException e) {
is1dot2 = false;
}
}
public static int computeStringWidth(FontMetrics fm,String str) {
int w[] = fm.getWidths();
int i,c;
int result = 0;
char ch;
for(i=0,c=str.length() ; i < c ; i++) {
ch = str.charAt(i);
if(ch > 255)
return fm.stringWidth(str);
else
result += w[(int)ch];
}
return result;
}
/**
* Compute and return the location of the icons origin, the
* location of origin of the text baseline, and a possibly clipped
* version of the compound labels string. Locations are computed
* relative to the viewR rectangle.
*/
public static String layoutCompoundLabel(
FontMetrics fm,
String text,
Icon icon,
int verticalAlignment,
int horizontalAlignment,
int verticalTextPosition,
int horizontalTextPosition,
Rectangle viewR,
Rectangle iconR,
Rectangle textR,
int textIconGap)
{
/* Initialize the icon bounds rectangle iconR.
*/
if (icon != null) {
iconR.width = icon.getIconWidth();
iconR.height = icon.getIconHeight();
}
else {
iconR.width = iconR.height = 0;
}
/* Initialize the text bounds rectangle textR. If a null
* or and empty String was specified we substitute "" here
* and use 0,0,0,0 for textR.
*/
boolean textIsEmpty = (text == null) || text.equals("");
if (textIsEmpty) {
textR.width = textR.height = 0;
text = "";
}
else {
textR.width = computeStringWidth(fm,text);
textR.height = fm.getHeight();
}
/* Unless both text and icon are non-null, we effectively ignore
* the value of textIconGap. The code that follows uses the
* value of gap instead of textIconGap.
*/
int gap = (textIsEmpty || (icon == null)) ? 0 : textIconGap;
if (!textIsEmpty) {
/* If the label text string is too wide to fit within the available
* space "..." and as many characters as will fit will be
* displayed instead.
*/
int availTextWidth;
if (horizontalTextPosition == CENTER) {
availTextWidth = viewR.width;
}
else {
availTextWidth = viewR.width - (iconR.width + gap);
}
if (textR.width > availTextWidth) {
String clipString = "...";
int totalWidth = computeStringWidth(fm,clipString);
int nChars;
for(nChars = 0; nChars < text.length(); nChars++) {
totalWidth += fm.charWidth(text.charAt(nChars));
if (totalWidth > availTextWidth) {
break;
}
}
text = text.substring(0, nChars) + clipString;
textR.width = computeStringWidth(fm,text);
}
}
/* Compute textR.x,y given the verticalTextPosition and
* horizontalTextPosition properties
*/
if (verticalTextPosition == TOP) {
if (horizontalTextPosition != CENTER) {
textR.y = 0;
}
else {
textR.y = -(textR.height + gap);
}
}
else if (verticalTextPosition == CENTER) {
textR.y = (iconR.height / 2) - (textR.height / 2);
}
else { // (verticalTextPosition == BOTTOM)
if (horizontalTextPosition != CENTER) {
textR.y = iconR.height - textR.height;
}
else {
textR.y = (iconR.height + gap);
}
}
if (horizontalTextPosition == LEFT) {
textR.x = -(textR.width + gap);
}
else if (horizontalTextPosition == CENTER) {
textR.x = (iconR.width / 2) - (textR.width / 2);
}
else { // (verticalTextPosition == RIGHT)
textR.x = (iconR.width + gap);
}
/* labelR is the rectangle that contains iconR and textR.
* Move it to its proper position given the labelAlignment
* properties.
*/
Rectangle labelR = iconR.union(textR);
int dx, dy;
if (verticalAlignment == TOP) {
dy = viewR.y - labelR.y;
}
else if (verticalAlignment == CENTER) {
dy = (viewR.y + (viewR.height / 2)) - (labelR.y + (labelR.height / 2));
}
else { // (verticalAlignment == BOTTOM)
dy = (viewR.y + viewR.height) - (labelR.y + labelR.height);
}
if (horizontalAlignment == LEFT) {
dx = viewR.x - labelR.x;
}
else if (horizontalAlignment == CENTER) {
dx = (viewR.x + (viewR.width / 2)) - (labelR.x + (labelR.width / 2));
}
else { // (horizontalAlignment == RIGHT)
dx = (viewR.x + viewR.width) - (labelR.x + labelR.width);
}
/* Translate textR and glypyR by dx,dy.
*/
textR.x += dx;
textR.y += dy;
iconR.x += dx;
iconR.y += dy;
return text;
}
/**
* Paint a component c on an abitrary graphics g in the
* specified rectangle. The component is reparented to a private
* container (whose parent becomes p) which prevents c.validate() and
* and c.repaint() calls from propogating up the tree. The intermediate
* container has no other effect.
*/
public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
getCellRendererPane(c, p).paintComponent(g, c, p, x, y, w, h,false);
}
public static void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
paintComponent(g, c, p, r.x, r.y, r.width, r.height);
}
/*
* Ensure that cell renderer c has a ComponentShell parent and that
* the shells parent is p.
*/
private static CellRendererPane getCellRendererPane(Component c, Container p) {
Container shell = c.getParent();
if (shell instanceof CellRendererPane) {
if (shell.getParent() != p) {
p.add(shell);
}
} else {
shell = new CellRendererPane();
shell.add(c);
p.add(shell);
}
return (CellRendererPane)shell;
}
/**
* A simple minded look and feel change: ask each node in the tree
* to updateUI(), i.e. to initialize its UI property with the
* current look and feel.
*/
public static void updateComponentTreeUI(Component c) {
updateComponentTreeUI0(c);
c.invalidate();
c.validate();
c.repaint();
}
private static void updateComponentTreeUI0(Component c) {
if (c instanceof JComponent) {
((JComponent) c).updateUI();
}
Component[] children = null;
if (c instanceof JMenu) {
children = ((JMenu)c).getMenuComponents();
}
else if (c instanceof Container) {
children = ((Container)c).getComponents();
}
if (children != null) {
for(int i = 0; i < children.length; i++) {
updateComponentTreeUI0(children[i]);
}
}
}
/**
* Causes <i>doRun.run()</i> to be executed asynchronously on the
* AWT event dispatching thread. This will happen after all
* pending AWT events have been processed. This method should
* be used when an application thread needs to update the GUI.
* In the following example the invokeAndWait() calls queues
* the doHelloWorld Runnable for the event dispatching thread and
* then prints a message.
* <pre>
* Runnable doHelloWorld = new Runnable() {
* public void run() {
* System.out.println("Hello World on " + Thread.currentThread());
* }
* };
*
* SwingUtilities.invokeAndWait(doHelloWorld);
* System.out.println("Waiting ... ");
* </pre>
* If invokeAndWait is called from the event dispatching thread,
* e.g. from a JButtons ActionListener, the <i>doRun.run()</i> will
* still be deferred till all pending events have been processed.
* Note that if the <i>doRun.run()</i> throws an uncaught exception
* the event dispatching thread will unwind (not the current thread).
* <p>
* Additional documentation and examples for this method can be
* found in <A HREF="http://java.sun.com/products/jfc/swingdoc-archive/threads.html">.
*
* @see #invokeAndWait
*/
public static void invokeLater(Runnable doRun) {
SystemEventQueueUtilities.postRunnable(doRun, null);
}
/**
* Causes <i>doRun.run()</i> to be executed synchronously on the
* AWT event dispatching thread. This call will block until
* all pending AWT events have been processed and (then)
* <i>doRun.run()</i> returns. This method should
* be used when an application thread needs to update the GUI.
* It should not be called from the EventDispatchThread.
* Here's an example that creates a new application thread
* that uses invokeAndWait() to print a string from the event
* dispatching thread and then, when that's finished, print
* a string from the application thread.
* <pre>
* final Runnable doHelloWorld = new Runnable() {
* public void run() {
* System.out.println("Hello World on " + Thread.currentThread());
* }
* };
*
* Thread appThread = new Thread() {
* public void run() {
* try {
* SwingUtilities.invokeAndWait(doHelloWorld);
* }
* catch (Exception e) {
* e.printStackTrace();
* }
* System.out.println("Finished on " + Thread.currentThread());
* }
* };
* appThread.start();
* </pre>
* Note that if the Runnable.run() method throws an uncaught exception
* (on the event dispatching thread) it's caught and rethrown, as
* an InvocationTargetException, on the callers thread.
* <p>
* Additional documentation and examples for this method can be
* found in <A HREF="http://java.sun.com/products/jfc/swingdoc-archive/threads.html">.
*
* @exception InterruptedException If we're interrupted while waiting for
* the event dispatching thread to finish excecuting <i>doRun.run()</i>
* @exception InvocationTargetException If <i>doRun.run()</i> throws
*
* @see #invokeLater
*/
public static void invokeAndWait(final Runnable doRun)
throws InterruptedException, InvocationTargetException
{
if(isEventDispatchThread ()) {
throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
}
Object lock = new Object() {
public String toString() {
return "SwingUtilities.invokeAndWait() lock for " + doRun;
}
};
Exception exc = null;
synchronized(lock) {
exc = SystemEventQueueUtilities.postRunnable(doRun, lock);
lock.wait();
}
if (exc != null) {
throw new InvocationTargetException(exc);
}
}
private static Class eventDispatchThreadClass = null;
/**
* @return true if the current thread is an AWT event dispatching thread.
*/
public static boolean isEventDispatchThread()
{
Thread currentThread = Thread.currentThread();
/* The first time we're called on what appears to be the event
* dispatching thread, we stash the threads class in
* eventDispatchThreadClass. Subsequently we effectively
* return eventDispatchThreadClass instanceof Thread.currentThread().
*/
if (eventDispatchThreadClass == null) {
Class currentThreadClass = currentThread.getClass();
/* This test is a crock. It's known to work on all of the popular
* JDK1.1 implementations available as of January 1998.
*/
if((currentThreadClass.getName().indexOf("EventDispatchThread") >= 0) ||
(currentThreadClass.getName().indexOf("JMEventQueue") >= 0)) {
eventDispatchThreadClass = currentThreadClass;
return true;
}
else {
return false;
}
}
return eventDispatchThreadClass.isInstance(currentThread);
}
/*
* --- Accessibility Support ---
*
*/
/**
* Get the index of this object in its accessible parent.
*
* @return -1 of this object does not have an accessible parent.
* Otherwise, the index of the child in its accessible parent.
*/
public static int getAccessibleIndexInParent(Component c) {
int index = -1;
Container parent = c.getParent();
if (parent != null) {
Component ca[] = parent.getComponents();
for (int i = 0; i < ca.length; i++) {
if (ca[i] instanceof Accessible) {
index++;
}
if (c.equals(ca[i])) {
return index;
}
}
}
return -1;
}
/**
* Returns the Accessible child contained at the local coordinate
* Point, if one exists.
*
* @return the Accessible at the specified location, if it exists
*/
public static Accessible getAccessibleAt(Component c, Point p) {
if (c instanceof Accessible) {
Accessible a = (Accessible) c;
if (a != null) {
AccessibleContext ac = a.getAccessibleContext();
if (ac != null) {
AccessibleComponent acmp;
Point location;
int nchildren = ac.getAccessibleChildrenCount();
for (int i=0; i < nchildren; i++) {
a = ac.getAccessibleChild(i);
if ((a != null)) {
ac = a.getAccessibleContext();
if (ac != null) {
acmp = ac.getAccessibleComponent();
if ((acmp != null) && (acmp.isShowing())) {
location = acmp.getLocation();
Point np = new Point(p.x-location.x,
p.y-location.y);
if (acmp.contains(np)){
return a;
}
}
}
}
}
}
}
return (Accessible) c;
} else {
Component ret = c;
if (!c.contains(p.x,p.y)) {
ret = null;
} else if (c instanceof Container) {
Container cnt = (Container) c;
int ncomponents = cnt.getComponentCount();
for (int i=0; i < ncomponents; i++) {
Component comp = cnt.getComponent(i);
if ((comp != null) && comp.isShowing()) {
Point location = comp.getLocation();
if (comp.contains(p.x-location.x,p.y-location.y)) {
ret = comp;
}
}
}
}
if (ret instanceof Accessible) {
return (Accessible) ret;
}
}
return null;
}
/**
* Get the state of this object.
*
* @return an instance of AccessibleStateSet containing the current state
* set of the object
* @see AccessibleState
*/
public static AccessibleStateSet getAccessibleStateSet(Component c) {
AccessibleStateSet states = new AccessibleStateSet();
if (c.isEnabled()) {
states.add(AccessibleState.ENABLED);
}
if (c.isFocusTraversable()) {
states.add(AccessibleState.FOCUSABLE);
}
if (c.isVisible()) {
states.add(AccessibleState.VISIBLE);
}
if (c.isShowing()) {
states.add(AccessibleState.SHOWING);
}
// [[[FIXME: WDW - for JDK1.2 this code can be replaced with
// c.hasFocus()]]]
for (Container p = c.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
if (((Window)p).getFocusOwner() == c) {
states.add(AccessibleState.FOCUSED);
}
}
}
if (c instanceof Accessible) {
AccessibleContext ac = ((Accessible) c).getAccessibleContext();
if (ac != null) {
Accessible ap = ac.getAccessibleParent();
if (ap != null) {
AccessibleContext pac = ap.getAccessibleContext();
if (pac != null) {
AccessibleSelection as = pac.getAccessibleSelection();
if (as != null) {
states.add(AccessibleState.SELECTABLE);
int i = ac.getAccessibleIndexInParent();
if (i >= 0) {
if (as.isAccessibleChildSelected(i)) {
states.add(AccessibleState.SELECTED);
}
}
}
}
}
}
}
if (c instanceof JComponent) {
if (((JComponent) c).isOpaque()) {
states.add(AccessibleState.OPAQUE);
}
}
return states;
}
/**
* Returns the number of accessible children in the object. If all
* of the children of this object implement Accessible, than this
* method should return the number of children of this object.
*
* @return the number of accessible children in the object.
*/
public static int getAccessibleChildrenCount(Component c) {
int count = 0;
if (c instanceof Container) {
Component[] children = ((Container) c).getComponents();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof Accessible) {
count++;
}
}
}
return count;
}
/**
* Return the nth Accessible child of the object.
*
* @param i zero-based index of child
* @return the nth Accessible child of the object
*/
public static Accessible getAccessibleChild(Component c, int i) {
if (c instanceof Container) {
Component[] children = ((Container) c).getComponents();
int count = 0;
for (int j = 0; j < children.length; j++) {
if (children[j] instanceof Accessible) {
if (count == i) {
return (Accessible) children[j];
} else {
count++;
}
}
}
}
return null;
}
/**
* Return the child component which has focus, if any. The HotJava
* SecurityManager forbids applet access to getFocusOwner(), so if the
* component is an applet, we check whether a JComponent has focus.
* Non-Swing components in an applet on HotJava are out-of-luck,
* unfortunately.
*/
public static Component findFocusOwner(Component c) {
if (c instanceof Window) {
return ((Window)c).getFocusOwner();
}
if (c instanceof JComponent && ((JComponent)c).hasFocus()) {
return c;
}
if (c instanceof Container) {
int n = ((Container)c).countComponents();
for (int i = 0; i < n; i++) {
Component focusOwner =
findFocusOwner(((Container)c).getComponent(i));
if (focusOwner != null) {
return focusOwner;
}
}
return null;
} else {
return null; // Component doesn't have hasFocus().
}
}
/**
* If c is a JRootPane descendant return its JRootPane ancestor.
* If c is a RootPaneContainer then return its JRootPane.
* @return the JRootPane for Component c or null.
*/
public static JRootPane getRootPane(Component c) {
if (c instanceof RootPaneContainer) {
return ((RootPaneContainer)c).getRootPane();
}
for( ; c != null; c = c.getParent()) {
if (c instanceof JRootPane) {
return (JRootPane)c;
}
}
return null;
}
/**
* @return the first ancestor of c that's a Window or the last Applet ancestor.
*/
public static Component getRoot(Component c) {
Component applet = null;
for(Component p = c; p != null; p = p.getParent()) {
if (p instanceof Window) {
return p;
}
if (p instanceof Applet) {
applet = p;
}
}
return applet;
}
// Don't use String, as it's not guaranteed to be unique in a Hashtable.
private static final Object sharedOwnerFrameKey =
new StringBuffer("SwingUtilities.sharedOwnerFrame");
/**
* Returns a toolkit-private, shared, invisible Frame
* to be the owner for JDialogs and JWindows created with
* null owners.
*/
static Frame getSharedOwnerFrame() {
Frame sharedOwnerFrame =
(Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey);
if (sharedOwnerFrame == null) {
sharedOwnerFrame = new Frame() {
public void show() {
// This frame can never be shown
}
public synchronized void dispose() {
try {
getToolkit().getSystemEventQueue();
super.dispose();
} catch (Exception e) {
// untrusted code not allowed to dispose
}
}
};
SwingUtilities.appContextPut(sharedOwnerFrameKey,
sharedOwnerFrame);
}
return sharedOwnerFrame;
}
// The following static var and methods are a temporary
// workaround for a Solaris bug where disposing modal dialogs
// can sometimes cause a segmentation violation. Once this
// AWT bug is fixed and available in browsers, we can remove
// this workaround.
private static final Object dialogsKey =
new StringBuffer("SwingUtilities.dialogs");
static JDialog getRecycledModalDialog(Frame frame, String title) {
Vector dialogs = (Vector)SwingUtilities.appContextGet(dialogsKey);
if (dialogs == null) {
dialogs = new Vector();
SwingUtilities.appContextPut(dialogsKey, dialogs);
}
JDialog dialog = null;
synchronized(dialogs) {
for(int i = 0; i < dialogs.size(); i++) {
dialog = (JDialog)dialogs.elementAt(i);
if (dialog.getParent() == frame) {
//System.out.println("Found available dialog: "+dialog);
dialogs.removeElement(dialog);
dialog.setTitle(title);
return dialog;
}
}
dialog = new JDialog(frame, title, true);
//System.out.println("Created new dialog: "+dialog);
}
return dialog;
}
static void recycleModalDialog(JDialog dialog) {
Vector dialogs = (Vector)SwingUtilities.appContextGet(dialogsKey);
synchronized(dialogs) {
dialog.getContentPane().removeAll();
dialogs.addElement(dialog);
}
}
/* Don't make these AppContext accessors public or protected --
* since AppContext is in sun.awt in 1.2, we shouldn't expose it
* even indirectly with a public API.
*/
static Hashtable appContextTable = new Hashtable(2);
static Object appContextGet(Object key) {
/*if[JDK1.2]
return sun.awt.AppContext.getAppContext().get(key);
else[JDK1.2]*/
return appContextTable.get(key);
/*end[JDK1.2]*/
}
static void appContextPut(Object key, Object value) {
/*if[JDK1.2]
sun.awt.AppContext.getAppContext().put(key, value);
else[JDK1.2]*/
appContextTable.put(key, value);
/*end[JDK1.2]*/
}
static void appContextRemove(Object key) {
/*if[JDK1.2]
sun.awt.AppContext.getAppContext().remove(key);
else[JDK1.2]*/
appContextTable.remove(key);
/*end[JDK1.2]*/
}
/**
* Marks the calling thread's stack frame as "privileged" on 1.2 and
* higher VMs. This call has no effect on 1.1.
*/
final static void beginPrivileged() {
/*if[JDK1.2]
java.security.AccessController.beginPrivileged();
end[JDK1.2]*/
}
/**
* Unmarks the calling thread's stack frame, indicating it is no longer
* "privileged". This call may only be done
* in the same frame as the <code>beginPrivileged</code> call. This
* call has no effect on 1.1.
*/
final static void endPrivileged() {
/*if[JDK1.2]
java.security.AccessController.endPrivileged();
end[JDK1.2]*/
}
}